<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/10/8
 * Time: 18:18
 */
define('ENV', 'test');
if (ENV == 'dev') {
    define('HOST', '127.0.0.1');
    define('PORT', '3307');
    define('USER', 'root');
    define('PWD', 'usbw');
    define('DBNAME', 'jhdb');
} elseif (ENV == 'test') {
    define('HOST', '127.0.0.1');
    define('PORT', '3306');
    define('USER', 'root');
    define('PWD', 'jh!@#2016');
    define('DBNAME', 'jhdb');
} else {
    define('HOST', '127.0.0.1');
    define('PORT', '3306');
    define('USER', 'root');
    define('PWD', 'jh!@#2016');
    define('DBNAME', 'jhdb');
}

if (!empty($_POST['code1'])) {
    $update_data['tongji'] = $_POST['code1'];
    $result = replace($update_data, 1);

}

if (!empty($_POST['code2'])) {
    $update_data['tongji'] = $_POST['code2'];
    $result = replace($update_data, 2);
}

if (!empty($_POST['code3'])) {
    $update_data['tongji'] = $_POST['code3'];
    $result = replace($update_data, 3);
}

exit(json_encode(array("msg" => "true")));





function replace($data, $id)
{
    $dsn = 'mysql:dbname=' . DBNAME . ';host=' . HOST . ';port=' . PORT;
    $user = USER;
    $password = PWD;
    $dbh = new PDO($dsn, $user, $password);
    $dbh->query("set names utf8");
    if (!is_array($data)) {
        return false;
    }

    if (isset($data['tongji'])) {
        $s1 = "'" . $data['tongji'] . "'";
    } else {
        $s1 = "''";
    }


//            $s2 = "'" . $value[1] . "'";
//            $s3 = "'" . $value[2] . "'";
//            $s4 = "'" . $value[3] . "'";
//            $s5 = "'" . $value[4] . "'";
//            $s6 = "'" . $value[5] . "'";
//            $s7 = "'" . $value[6] . "'";
//            $s8 = "'" . $value[7] . "'";

    $value_str_s = '(' . $id . ',' . $s1 . ')';


    $sql = "REPLACE INTO tong_ji (`id`,`tongji`) VALUES " . $value_str_s;


    $rt = $dbh->exec($sql);
    return $rt;
}